﻿/*
 ***************************************************************************
 * Microsoft Visual C# .NET
 * 名称：       App_Input_HealthDietAdviceForm
 * 类型：       控制层
 * 功能说明：   饮食健康画面
 * 作者：       王剑华
 * 创建日期：   2007/09/11
 * 修改日期     修改者      修改内容
 * 
 * 
 * ---
 ***************************************************************************
*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Shpm.App.Input.Dao;
using Shpm.App.Input.Bean;
using Shpm.System.Db;
using Shpm.System;
using Shpm.App.Commons.Db.Beans.Inp;
using Shpm.System.Web.Control;
using Shpm.App.Commons.Db.Beans.Master;
using Shpm.App.Master.Dao;
using Shpm.App.Commons.Utils;
using log4net;
using Shpm.System.Validator;

public partial class App_Input_HealthDietAdvice : System.Web.UI.Page
{
    HealthDietAdviceDao dao = new HealthDietAdviceDao();
    // log
    protected static ILog log = LogManager.GetLogger(typeof(App_Input_HealthDietAdvice));


    protected void Page_Load(object sender, EventArgs e)
    {
      
        if (IsPostBack == false)
        {
            // 取得当前登录的医生id信息
            SysUserManager manager = new SysUserManager(Context);
            SysUserBean sysUserBean = manager.getSysUserBean();
            MstSysUserBean mstSysUserBean =
                sysUserBean == null ? null : sysUserBean.getMstSysUserBean();
            MstEmployeeBean mstEmpBean =
                sysUserBean == null ? null : sysUserBean.getEmployeeBean();
            string loginId = mstSysUserBean == null ? "" : mstSysUserBean.SysUserId;
            string emplyId = mstSysUserBean == null ? "" : mstSysUserBean.EmployeeId;
            string emplyJob = mstSysUserBean == null ? "" : mstEmpBean.EmployeeJob;
            string emplyLevel = mstSysUserBean == null ? "" : mstEmpBean.EmployeeLevel;
            string emplyRole = mstEmpBean == null ? "" : mstEmpBean.Role;
            ViewState["DOCTOR_ID"] = emplyId;
            ViewState["MEMBER_NO"] = (string)Request.QueryString["MEMBER_NO"];
        }
    }

    //获取传递数据库数据
    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);

        HealthDietAdviceBean paramBean = new HealthDietAdviceBean();
        paramBean.MemberNo = Convert.ToString(ViewState["MEMBER_NO"]);

        // edit
        paramBean = dao.Load(paramBean);
        if (paramBean != null)
        {
            LblDoctorId.Text = paramBean.DoctorId;
            LblMemberNo.Text = paramBean.MemberNo;
            //LblMemberNo.Text = Convert.ToString(ViewState["MEMBER_NO"]);
            LblMemberName.Text = paramBean.NameC;
            LblSex.Text = paramBean.Sex;
            LblTel.Text = paramBean.Tel;
            LblApartmentNo.Text = paramBean.ApartmentNo;

        }

        // 获取传递数据字典数据
        CblFoodType.Items.Clear();
        bindCommonDicCats();
        IList List = dao.LoadFoodType(paramBean);
        for (int i = 0; i < List.Count; i++)
        {
            paramBean = (HealthDietAdviceBean)List[i];
            if (CblFoodType.Items.Count > 0)
            {
                ListItem selectItem = CblFoodType.Items.FindByValue(paramBean.FoodId);
                if (selectItem != null) selectItem.Selected = true;
            }
        }
    }


    private void bindCommonDicCats()
    {
        CommonDictionaryBean beanComDict = new CommonDictionaryBean();
        IList lstCat = null;

        // 饮食建议
        beanComDict.Catalog = "M_MEMBER_FOOD.FOOD_TYPE";
        lstCat = dao.FindByCatalog(beanComDict);
        bindDataToControl("CblFoodType", lstCat);
    }


    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        try
        {
            // do validate check
            ComValidateChecker vd = new ComValidateChecker();
            vd.CheckItems.Add(new CheckRequired(LblMemberNo.Text, new ComMessage("E0094", "登入失败<br>会员编号")));

            if (!vd.DoValidator())
            {
                ((Design_MasterPage)this.Master).InfoMessage = vd.ErrorMessage;
                return;
            }

            // check authority
            SysUserManager manager = new SysUserManager(Context);
            SysUserBean sysUserBean = manager.getSysUserBean();
            MstSysUserBean mstSysUserBean =
                sysUserBean == null ? null : sysUserBean.getMstSysUserBean();
            MstEmployeeBean mstEmpBean =
                sysUserBean == null ? null : sysUserBean.getEmployeeBean();
            string loginId = mstSysUserBean == null ? "" : mstSysUserBean.SysUserId;
            string emplyId = mstSysUserBean == null ? "" : mstSysUserBean.EmployeeId;

            HealthDietAdviceDao dao = new HealthDietAdviceDao();
            MstMemberDoctorBean beanCheck = new MstMemberDoctorBean();
            beanCheck.MemberNo = LblMemberNo.Text;
            beanCheck.DoctorId = emplyId;
            IList listChk = dao.CheckMemberDoctor(beanCheck);
            if (listChk == null || listChk.Count == 0)
            {
                ((Design_MasterPage)this.Master).InfoMessage =
                    HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "E0901").ToString();
                return;
            }
            

            //数据传递
            HealthDietAdviceBean paramBean1 = new HealthDietAdviceBean();
            
            paramBean1.MemberNo = LblMemberNo.Text;
            IList listFood = null;
            listFood = new ArrayList();
            for (int i = 0; i < CblFoodType.Items.Count; i++)
            {
                if (CblFoodType.Items[i].Selected)
                {
                    HealthDietAdviceBean paramBean = new HealthDietAdviceBean();
                    paramBean.MemberNo = LblMemberNo.Text;
                    paramBean.FoodId = CblFoodType.Items[i].Value;
                    paramBean.UserId = loginId;
                    listFood.Add(paramBean);
                }
            }
            log.Info("***** Start UPDATE INSERT *****");
            dao.InsertUpdate(paramBean1,listFood);
            ((Design_MasterPage)this.Master).InfoMessage =
            HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0001").ToString();
        }
        catch (Exception ex)
        {
            log.Fatal(ex.StackTrace);
            log.Fatal(ex.Message);
            throw ex;
        }
    }
    
    
    
    // bind db data to selected controls
    private void bindDataToControl(string ControlId, IList LstData)
    {
        if (LstData == null || LstData.Count == 0) return;

        foreach (CommonDictionaryBean bean in LstData)
        {
            if (this.Master.FindControl("ContentPlaceHolder1").FindControl(ControlId) is DropDownList)
            {
                ((DropDownList)this.Master.FindControl("ContentPlaceHolder1").FindControl(ControlId)).Items.Add(new ListItem(bean.Content, bean.DictNo.ToString()));
            }
            else if ((this.Master.FindControl("ContentPlaceHolder1").FindControl(ControlId) is CheckBoxList)
                || (this.Master.FindControl("ContentPlaceHolder1").FindControl(ControlId) is RadioButtonList))
            {
                ((ListControl)this.Master.FindControl("ContentPlaceHolder1").FindControl(ControlId)).Items.Add(new ListItem(bean.Content, bean.DictNo.ToString()));
            }
        }
    }

}
